﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace m.toidihoilo
{
    public partial class Default : System.Web.UI.Page
    {
        public int page = 1;
        public int totalpage;
        public bool sa = false;
        public string html_pager = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["sa"] != null)
                sa = Convert.ToBoolean(Session["sa"]);
            if (Request.QueryString["page"] != null)
            {
                int.TryParse(Request.QueryString["page"].ToString(), out page);
            }
            var db = new toidihoiloEntitiesMobile();
            var rs = new List<Bribe>();

            if (sa == false)
            {
                rs =
                    db.Bribes.Where(t => t.Enable != false).OrderByDescending(t => t.DateReport).Skip((page - 1) * 10).
                        Take(10)
                        .ToList();
            }
            else
            {
                rs =
                    db.Bribes.OrderByDescending(t => t.DateReport).Skip((page - 1) * 10).
                        Take(10)
                        .ToList();
            }
            foreach (var bribe in rs)
            {
                if (HttpUtility.HtmlDecode(bribe.Content).Length > 400)
                    bribe.Content = HttpUtility.HtmlDecode(bribe.Content).Substring(0, 400) + "...";
                double d = 0;
                bool abc = double.TryParse(bribe.AmountPaid, out d);
                if (abc)
                    bribe.AmountPaid = String.Format("{0:0,0}", d) + " VND";
            }
            Repeater1.DataSource = rs;
            Repeater1.DataBind();
            //phan trang html
            var countRow = 0;
            if (sa == false)
            {
                countRow = db.Bribes.Where(t => t.Enable != false).Count();
            }
            else
            {
                countRow = db.Bribes.Count();
            }

            
            totalpage = countRow % 10 == 0 ? countRow / 10 : (countRow / 10) + 1;
            var current_page = page;
            var total_page = totalpage;
            //show pager
            var start = current_page - 5;
            if(start < 0)
            {
                start = 1;
            }
            var end = current_page + 5;
            if(current_page < 5)
            {
                end = current_page + 9;
            }
            if(end > total_page)
            {
                end = total_page;
            }
            //foeach
                        
            //trang dau
             //trang truoc
            if(current_page > 1)
            {
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx\">« đầu</a>";
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?page=" + (current_page - 1) + "\">‹ trước</a></li>";
            }
           
            for(var i=start; i<=end; i++)
            {
                html_pager += "";                
                if(i == current_page)
                {
                    html_pager += "<a style=\"color: #FF3300;\" data-role=\"button\" title=\"\" href=\"javascript:void(0);\">" + current_page + "</a>";
                }else
                {
                    html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?page=" + i + "\">" + i + "</a>";
                }
            }
            //sau va cuoi cung
            if(current_page < total_page)
            {
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?page=" + (current_page + 1) + "\">sau ›</a>";
                html_pager += "<a data-role=\"button\" title=\"\" href=\"Default.aspx?page=" + total_page + "\">cuối »</a>";
            }            
            //bind html            
        }
    }
}